﻿using System;
using System.Globalization;
using System.Text;

namespace Smart.Web.Mobile
{
    /// <summary>
    /// 
    /// </summary>
    public static class MobileUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string ErasePictgram(string src)
        {
            if( String.IsNullOrEmpty( src ) )
            {
                return src;
            }

            StringBuilder sb = new StringBuilder();
            foreach( char c in src )
            {
                if( ( c < 0xE000 ) || ( c >= 0xF900 ) )
                {
                    sb.Append( c );
                }                
            }
            return sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static char HexToUnicode(string hex)
        {
            byte[] bytes = HexToBytes( hex );
            char c;
            if( bytes.Length == 1 )
            {
                c = (char)bytes[ 0 ];
            }
            else if( bytes.Length == 2 )
            {
                c = (char)( ( bytes[ 0 ] << 8 ) + bytes[ 1 ] );
            }
            else
            {
                throw new ArgumentException( hex );
            }

            return c;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static byte[] HexToBytes(string hex)
        {
            hex = hex.Trim();

            byte[] bytes = new byte[ hex.Length / 2 ];

            for( int index = 0; index < bytes.Length; index++ )
            {
                bytes[ index ] = byte.Parse( hex.Substring( index * 2, 2 ), NumberStyles.HexNumber );
            }

            return bytes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sjis"></param>
        /// <returns></returns>
        public static string SjisToHex(byte[] sjis)
        {
            if( sjis == null )
            {
                return "";
            }

            return String.Format( "{0:X2}{1:X2}", sjis[0], sjis[1] );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string UnicodeToHex(char c)
        {
            return String.Format( "{0:X4}", (int)c );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="chars"></param>
        /// <returns></returns>
        public static string UnicodeToHex(char[] chars)
        {
            StringBuilder sb = new StringBuilder();
            foreach( char c in chars )
            {
                if( c < 0x80 )
                {
                    sb.Append( String.Format( "{0:X2}", (int)c ) );
                }
                else
                {
                    sb.Append( String.Format( "{0:X4}", (int)c ) );
                }
            }

            return sb.ToString();
        }
    }
}
